const width = document.documentElement.clientWidth;
const height = document.documentElement.clientHeight;

// console.log(width, height);

if (width < 1024 || height < 768) {
    const count = window.sessionStorage.getItem("alert");
    if (count !== "1") {
        alert("系统检测到您正在小窗口设备上访问此网站，效果可能显示不佳。建议使用PC设备访问此网站。");
        window.sessionStorage.setItem("alert", "1");
    }
}

layui.use(['layer'], function () {
    const layer = layui.layer;
    const $ = layui.$;
    const fullUrl = window.location.href;
    const url = fullUrl.substring(fullUrl.lastIndexOf("/"));
    let header = window.sessionStorage.getItem("header");
    if (header === "datapre") {
        $("#datapre").parent().addClass("layui-this");
        $("#console").parent().removeClass("layui-this");
        $("#apiadmin").parent().removeClass("layui-this");
    } else if (header === 'console') {
        $("#datapre").parent().removeClass("layui-this");
        $("#console").parent().addClass("layui-this");
        $("#apiadmin").parent().removeClass("layui-this");
    } else if (header === 'apiadmin') {
        $("#datapre").parent().removeClass("layui-this");
        $("#console").parent().removeClass("layui-this");
        $("#apiadmin").parent().addClass("layui-this");
    }

    $("#datapre").click(() => {
        if (url !== "/datapre.html") {
            window.location.href = "/datapre.html";
            window.sessionStorage.setItem("header", "datapre");
        }
    });

    $("#console").click(() => {
        if (url !== "/index.html") {
            window.location.href = "/index.html";
            window.sessionStorage.setItem("header", "console");
        }
    })

    $("#apiadmin").click(() => {
        if (url !== "/apiadmin.html") {
            window.location.href = "/apiadmin.html";
            window.sessionStorage.setItem("header", "apiadmin");
        }
    })

    $("#howtouse").click(() => {
        if (url !== "/quickStart.html") {
            window.open("/html/quickStart.html");
        }
    })

    $("#logout").on('click', function () {
        layer.confirm('确定要退出?　正在执行的爬虫任务不会暂停。再次访问需要重新登录。', {
            btn: ['确定', '再想想'] //可以无限个按钮

        }, function (index, layero) {
            fetch("/logout").then(res => res.json()).then(json => {
                top.location.href = '/login';
            }).catch(err => {
                top.location.href = '/login';
                console.log("error", err);
            })
        }, function (index) {
            console.log(index)
        });
    })

    $("#changepwd").on('click', function () {
        top.location.href = "/resetpwd.html";
        $("#datapre").parent().removeClass("layui-this");
        $("#console").parent().removeClass("layui-this");
        $("#apiadmin").parent().removeClass("layui-this");
        window.sessionStorage.removeItem("header");
    })
})